<link rel="stylesheet" type="text/css" href="../stylesheet/style.css"/>
<?php
/*
* @author Nguyen Xuan Hong
* @copyright 2011
*/

require_once('../includes/discuss.tbl.php');

if($_GET['mode'] == 'listdiscuss')
{
	$pClsDiscuss = new clsDiscussTable();	
	if($pClsDiscuss->avaibleSubmit()){
		$pClsDiscuss->loadBuffer();
		$pClsDiscuss->insertDB();
		$id = $pClsDiscuss->value('id');
   		
		$image = upload_file("Upload/", 'imgbrowser_image', $pClsDiscuss->value('image'), "dis".$id);
		if($image != "")
		{
			$pClsDiscuss->setvalue('image', $image);
		} 
		
		$pClsDiscuss->updateDB('id',$id);
		
		echo acpredirect();
		exit();
	}
	echo $pClsDiscuss->createForm('Thêm bản tin');
	
	require_once('../includes/templates.php');
	$table = new clsTable('lstdiscuss',array(array("ID", "1"),array("Tiêu dề tin", 0),array("Sửa", 1),array("Xóa", 1)));
	$list_discuss = $pClsDiscuss->listobj(10);
	foreach($list_discuss as $discuss){
		$html_remove =      '<a href="?page=discuss&mode=deletediscuss&id='.$discuss->value('id').'">Xóa</a>';
		$html_edit = 	'<a href="?page=discuss&mode=editdiscuss&id='.$discuss->value('id').'">Sửa</a>';	
		$table->addRow($discuss->value('id'),array($discuss->value('id'),$discuss->value('title'), $html_edit,$html_remove));
	}
	echo $table->putHTML(true);
	exit();
}

if($_GET['mode'] == 'deletediscuss'){		
	// Redefine insert
	$deleteid = sget('id');			
	$pClsDiscuss = new clsDiscussTable($deleteid);
	if(!$_POST['submit']){
		?>
        
	<center><div class="notification warning">
        <span></span>
        <p><strong>Warning!</strong> 
		<div class="text">Bạn có muốn xóa Bản tin này không</div> 
            <div class="btn"><form method='post'><input type='submit' name='submit' value = 'Xóa' /></form></div>
         </p>
	</div></center>
        <?
		exit();
	}
	$pClsDiscuss->removeDB('id', $deleteid);
       header("location:index.php?page=discuss&mode=listdiscuss");
	exit();
}

if($_GET['mode'] == 'editdiscuss')
{
	$edit = (int)sget('id');
	$pClsDiscuss = new clsDiscussTable($deleteid);
	if($pClsDiscuss->avaibleSubmit()){
		$pClsDiscuss->loadBuffer();
        $image = upload_file("Upload/", 'imgbrowser_image', $pClsDiscuss->value('image'), "dis".$edit);
        if($image != "")
        {
        	$pClsDiscuss->setvalue('image', $image);
        } 
		$pClsDiscuss->updateDB('id',$edit);
        echo acpredirect('page=discuss&mode=listdiscuss');
		exit();
	}
	$pClsDiscuss->loadDB('id',$edit);
       echo $pClsDiscuss->createForm('Sửa bản tin');
	exit();
}
?> 
